﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MM.Common.DBBase;
using System.Data;

namespace MM.Common.Base
{
    public static class BaseDataHelper
    {
        // Methods
        public static bool CheckComputerUsed(string node, string role_list)
        {
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("node", node), new InputDataParam("roles", role_list) });
            int data = 0;
            SystemInfo.DbData.ExecuteScalar<int>("SELECT COUNT(*) FROM MXHLY_SYS_COMPUTER_ROLE WHERE NODE = :node AND INSTR(:roles ,ROLE_CODE) > 0", parms, ref data);
            return (data == 1);
        }

        public static EControlPropertyStatus GetFunctionPointProperty(string code, string name)
        {
            if (!string.IsNullOrEmpty(code))
            {
                DataTable dt = new DataTable();
                DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("code", code) });
                string data = " ";
                string commandtext = "SELECT DEFAULT_PROPERTY FROM MXHLY_SYS_FUNCTION_POINT WHERE FUNCTION_POINT_CODE = :code";
                SystemInfo.DbData.ExecuteScalar<string>(commandtext, parms, ref data);
                if (data == null)
                {
                    data = " ";
                }
                commandtext = "SELECT ROLE_CODE, PROPERTY FROM MXHLY_SYS_FUNCTION_POINT_ROLE WHERE FUNCTION_POINT_CODE = :code";
                SystemInfo.DbData.Retrieve(commandtext, parms, ref dt);
                string str3 = " ";
                parms = new DataParamCol(new DataParam[] { new InputDataParam("emp_id", SystemInfo.OperId), new InputDataParam("code", code) });
                commandtext = "SELECT PROPERTY FROM MXHLY_SYS_FUNCTION_POINT_EMP WHERE EMP_ID = :emp_id AND FUNCTION_POINT_CODE = :code ";
                SystemInfo.DbData.ExecuteScalar<string>(commandtext, parms, ref str3);
                if (str3 == null)
                {
                    str3 = " ";
                }
                if (str3.CompareTo(data) > 0)
                {
                    data = str3;
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string str4 = dt.Rows[i]["PROPERTY"].ToString();
                    if (str4.CompareTo(data) > 0)
                    {
                        data = str4;
                    }
                }
                if (data == " ")
                {
                    data = "W";
                    try
                    {
                        string paramValue = code.Substring(0, code.LastIndexOf("-"));
                        string str6 = "INSERT INTO MXHLY_SYS_FUNCTION_POINT (FUNCTION_POINT_CODE ,FUNCTION_POINT_NAME ,FORM_CODE ,DEFAULT_PROPERTY) \r\n   VALUES (:pcode ,:pname ,:fcode ,:property) ";
                        parms = new DataParamCol(new DataParam[] { new InputDataParam("pcode", code), new InputDataParam("pname", name), new InputDataParam("fcode", paramValue), new InputDataParam("property", "W") });
                        SystemInfo.DbData.TransactionBegin();
                        if (SystemInfo.DbData.ExecuteSql(str6, parms) == 1)
                        {
                            SystemInfo.DbData.TransactionCommit();
                        }
                        else
                        {
                            SystemInfo.DbData.TransactionRollback();
                        }
                    }
                    catch
                    {
                    }
                }
                switch (data)
                {
                    case "H":
                        return EControlPropertyStatus.Hide;

                    case "R":
                        return EControlPropertyStatus.Read;

                    case "W":
                        return EControlPropertyStatus.Write;
                }
            }
            return EControlPropertyStatus.None;
        }

        public static DataTable GetMenu()
        {
            string commandtext = "";
            DataTable dt = new DataTable();
            if (SystemInfo.DbData.GetType().ToString().IndexOf("Oracle") >= 0)
            {
                commandtext = "SELECT M.ORDER_NO, M.TITLE, M.FUNCTION_CODE, M.ICO,\r\n  M.PROMPT, M.OPEN_FLAG ,F.OBJECT_NAME, F.PARAMETER, MO.MODULE_FILENAME ,MO.SERIAL ,F.USED_FLAG \r\n   FROM MXHLY_SYS_FUNCTION F, MXHLY_SYS_MENU M ,MXHLY_SYS_MODULE MO \r\n   WHERE ( F.FUNCTION_CODE (+) = M.FUNCTION_CODE) AND MO.MODULE_CODE (+) = F.MODULE_CODE AND ( M.SYSTEM_CODE = :system_code ) \r\n      AND ( M.FUNCTION_CODE IS NULL OR (SELECT COUNT(*) FROM MXHLY_SYS_FUNCTION_ROLE WHERE FUNCTION_CODE = M.FUNCTION_CODE AND INSTR(:roles ,ROLE_CODE) > 0 ) > 0 \r\n            OR (SELECT COUNT(*) FROM MXHLY_SYS_FUNCTION_EMP WHERE FUNCTION_CODE = M.FUNCTION_CODE AND EMP_ID = :emp_id) > 0 ) \r\n   ORDER BY M.ORDER_NO ASC ";
            }
            else
            {
                //sqlserver 使用的是视图
                commandtext = "select * from View_Menu ORDER BY ORDER_NO";
            }
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("system_code", SystemInfo.SystemCode), new InputDataParam("roles", SystemInfo.OperRoleList), new InputDataParam("emp_id", SystemInfo.OperId) });
            SystemInfo.DbData.Retrieve(commandtext, parm, ref dt);
            return dt;
        }

        public static string GetOperRoleList()
        {
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("emp_id", SystemInfo.OperId) });
            DataTable dt = new DataTable();
            StringBuilder builder = new StringBuilder();
            SystemInfo.DbData.Retrieve("SELECT ROLE_CODE FROM MXHLY_SYS_ROLE_EMP WHERE EMP_ID = :emp_id", parm, ref dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                builder.Append(dt.Rows[i]["ROLE_CODE"].ToString());
                builder.Append(" ");
            }
            return builder.ToString();
        }

        public static bool IsNewComputer()
        {
            int data = 0;
            SystemInfo.DbData.ExecuteScalar<int>("SELECT COUNT(*) FROM MXHLY_SYS_COMPUTER WHERE COMPUTER_CODE = :node", new DataParamCol(new DataParam[] { new InputDataParam("node", SystemInfo.ComputerID) }), ref data);
            if (data != 1)
            {
                SystemInfo.DbData.ExecuteSql("INSERT INTO MXHLY_SYS_COMPUTER (COMPUTER_CODE ,COMPUTER_NAME ,IP) VALUES (:node ,:name ,:ip)", new DataParamCol(new DataParam[] { new InputDataParam("node", SystemInfo.ComputerID), new InputDataParam("name", SystemInfo.ComputerName), new InputDataParam("ip", SystemInfo.ComputerIp) }));
            }
            return (data == 1);
        }

        public static bool OperLogin(string oper_code, string password, ref string error_message)
        {
            bool flag = false;
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("emp_code", oper_code) });
            DataTable dt = new DataTable();
            if (SystemInfo.DbData.Retrieve("SELECT * FROM MXH_EMPLOYEE WHERE EMP_CODE = :emp_code", parm, ref dt) && (dt.Rows.Count == 1))
            {
                if (dt.Rows[0]["USED_FLAG"].ToString() != "Y")
                {
                    error_message = "该用户已经停用， 请与管理员联系!";
                    return flag;
                }
                string key = dt.Rows[0]["PASSWORD"].ToString();
                Password password2 = new Password();
                if (password2.Decrypting(key) == password)
                {
                    SystemInfo.OperId = Converts.ConverToInt(dt.Rows[0]["EMP_ID"], 0);
                    SystemInfo.OperCode = Converts.ConverToString(dt.Rows[0]["EMP_CODE"], "");
                    SystemInfo.OperName = dt.Rows[0]["EMP_NAME"].ToString();
                    SystemInfo.OperDepartId = Converts.ConverToInt(dt.Rows[0]["DEPT_ID"], 0);
                    SystemInfo.OperInputCode = Converts.ConverToString(dt.Rows[0]["INPUT_CHOICE"], "1");
                    SystemInfo.OperIsAdmin = dt.Rows[0]["ADMIN_FLAG"].ToString() == "Y";
                    SystemInfo.OperGroupId = Converts.ConverToInt(dt.Rows[0]["EMP_GROUP_ID"], 0);
                    string data = "";
                    if (SystemInfo.DbData.ExecuteScalar<string>("SELECT DEPT_NAME FROM MXHLY_DIC_DEPT WHERE DEPT_ID = :id", new DataParamCol(new object[] { "id", SystemInfo.OperDepartId }), ref data))
                    {
                        if (data == null)
                        {
                            SystemInfo.OperDepartName = "";
                        }
                        else
                        {
                            SystemInfo.OperDepartName = data;
                        }
                    }
                    if (SystemInfo.DbData.ExecuteScalar<string>("SELECT EMP_GROUP_NAME FROM MXHLY_DIC_EMP_GROUP WHERE EMP_GROUP_ID = :id", new DataParamCol(new object[] { "id", SystemInfo.OperGroupId }), ref data))
                    {
                        if (data == null)
                        {
                            SystemInfo.OperGroupName = "";
                        }
                        else
                        {
                            SystemInfo.OperGroupName = data;
                        }
                    }
                    SystemInfo.OperIsDoctor = dt.Rows[0]["DOCTOR_FLAG"].ToString() == "Y";
                    if (SystemInfo.OperIsDoctor)
                    {
                        DataParamCol col2 = new DataParamCol(new DataParam[] { new InputDataParam("eid", SystemInfo.OperId) });
                        string commandtext = "select * from MXHLY_DIC_DOCTOR_INFO where EMP_ID=:eid";
                        DataTable table2 = new DataTable();
                        SystemInfo.DbData.Retrieve(commandtext, col2, ref table2);
                        if (table2.Rows.Count > 0)
                        {
                            SystemInfo.Prescription = table2.Rows[0]["PRESCRIPTION"].ToString();
                            SystemInfo.Narcotic = table2.Rows[0]["NARCOTIC"].ToString() == "Y";
                            SystemInfo.Psychotropic = table2.Rows[0]["PSYCHOTROPIC"].ToString() == "Y";
                            SystemInfo.Poison = table2.Rows[0]["POISON"].ToString() == "Y";
                            SystemInfo.SkinTest = table2.Rows[0]["SKIN_TEST"].ToString() == "Y";
                            SystemInfo.AntibioticLevels = int.Parse(table2.Rows[0]["ANTIBIOTIC_LEVELS"].ToString());
                        }
                    }
                    return true;
                }
                error_message = "错误的口令!";
                return flag;
            }
            error_message = "无此用户号!";
            return flag;
        }
    }
}
